package com.onlyxiahui.common.action.description.handler.impl.validate;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;

import com.onlyxiahui.common.action.description.bean.PropertyData;
import com.onlyxiahui.common.action.description.bean.ValidateData;
import com.onlyxiahui.common.action.description.box.data.ValidationData;
import com.onlyxiahui.common.action.description.util.ActionAnnotationUtil;

/**
 * Description <br>
 * Date 2020-04-06 16:50:30<br>
 * 
 * @author XiaHui [onlovexiahui@qq.com]<br>
 * @since 1.0.0
 */

public class ValidationBase {
	public static void set(ValidationData vd, PropertyData propertyData, Annotation[] as) {
		String annotation = vd.getAnnotation();
		String repeatableAnnotation = vd.getRepeatableAnnotation();
		String repeatablePropertyName = vd.getRepeatablePropertyName();

		boolean has = ActionAnnotationUtil.hasAnnotation(annotation, as);
		if (has) {
			Annotation a = ActionAnnotationUtil.getAnnotation(annotation, as);
			set(propertyData, vd, a);
		}
		has = ActionAnnotationUtil.hasAnnotation(repeatableAnnotation, as);
		if (has) {
			Object repeatable = ActionAnnotationUtil.getValue(repeatableAnnotation, repeatablePropertyName, as);

			if (repeatable instanceof Annotation[]) {
				Annotation[] array = (Annotation[]) repeatable;
				for (Annotation a : array) {
					set(propertyData, vd, a);
				}
			}
		}
	}

	public static void set(PropertyData propertyData, ValidationData vd, Annotation a) {
		String validPropertyName = vd.getValidPropertyName();

		String validType = vd.getValidType();
		String validDefaultValue = vd.getValidDefaultValue();

		String groupPropertyName = vd.getGroupPropertyName();
		String validatorName = vd.getValidatorName();

		List<String> groups = new ArrayList<>();

		if (null != groupPropertyName && !groupPropertyName.isEmpty()) {
			Object value = ActionAnnotationUtil.getValue(groupPropertyName, a);
			if (null != value) {
				if (value instanceof Object[]) {
					Object[] array = (Object[]) value;
					for (Object v : array) {
						if (null != v) {
							groups.add(v.toString());
						}
					}
				} else {
					groups.add(value.toString());
				}
			}
		}

		if (null == validPropertyName || validPropertyName.isEmpty()) {
			ValidateData data = new ValidateData();
			data.setValidatorName(validatorName);
			data.setType(validType);
			data.setGroups(groups);
			data.setValue(validDefaultValue);

			propertyData.addValidate(data);
		} else {
			Object value = ActionAnnotationUtil.getValue(validPropertyName, a);

			if (null == value) {

				ValidateData data = new ValidateData();
				data.setValidatorName(validatorName);
				data.setType(validType);
				data.setGroups(groups);
				data.setValue(validDefaultValue);

				propertyData.addValidate(data);

			} else if (value instanceof Object[]) {
				Object[] array = (Object[]) value;
				for (Object v : array) {
					if (null != v) {
						ValidateData data = new ValidateData();
						data.setValidatorName(validatorName);
						data.setType(validType);
						data.setGroups(groups);
						data.setValue(v.toString());

						propertyData.addValidate(data);
					}
				}
			} else {
				ValidateData data = new ValidateData();
				data.setValidatorName(validatorName);
				data.setType(validType);
				data.setGroups(groups);
				data.setValue(value.toString());

				propertyData.addValidate(data);
			}
		}
	}
}
